package com.vip.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.base.service.ISysDictService;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.Result;
import com.vip.common.response.Results;
import com.vip.database.dto.dict.AddDictDTO;
import com.vip.database.dto.dict.GetDictDTO;
import com.vip.database.entity.SysDict;
import com.vip.database.mapper.SysDictMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author LEON
 * @since 2020-04-24
 */
@Slf4j
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {
    @Autowired
    private SysDictMapper dictMapper;

    @Override
    public Result<List<GetDictDTO>> getDictList(Integer pageNo, Integer pageSize) {
        try {
            PageHelper.startPage(pageNo, pageSize);
            List<GetDictDTO> result = dictMapper.getDictList();
            PageInfo<GetDictDTO> pageInfo = new PageInfo<>(result);
            return Results.newSuccessResult(pageInfo.getList(), "查询成功");
        } catch (Exception e) {
            log.error("获取字典列表数据出错！", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<String> addNewDict(AddDictDTO dto) {
        try {
            Result<Boolean> isExistResult =  dictIsExist(dto.getCode());
            if(isExistResult.isSuccess()){
                if(!isExistResult.getData()){
                    SysDict dict = new SysDict();
                    BeanUtils.copyProperties(dto, dict);
                    dictMapper.insert(dict);
                    return Results.newSuccessResult(dict.getId(), "添加成功");
                }
            }
            return Results.newFailResult(isExistResult.getErrorCode(), isExistResult.getDescription());
        } catch (Exception e) {
            log.error("新增字典出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "添加失败");
    }

    @Override
    public Result<Boolean> dictIsExist(String code) {
        try {
            // 如果查询结果为空则表示该字典或手机号不存在，否则存在
            if (dictMapper.getDictIsExist(code)) {
                return Results.newSuccessResult(true, "该字典名已被注册，换个新的试试");
            } else {
                return Results.newSuccessResult(false);
            }
        } catch (Exception e) {
            log.error("查询字典出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<String> updateDict(AddDictDTO dictDto) {
        try {
            SysDict dict = new SysDict();
            BeanUtils.copyProperties(dictDto, dict);
            dictMapper.updateById(dict);
            return Results.newSuccessResult(dict.getId(), "更新成功");
        } catch (Exception e) {
            log.error("更新字典出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "更新失败");
    }

    @Override
    public Result<List<GetDictDTO>> getDictChildList(String code) {
        try {
            List<GetDictDTO> result = dictMapper.getDictChildList(code);
            return Results.newSuccessResult(result, "查询成功");
        } catch (Exception e) {
            log.error("查询字典子数据出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }
}
